package com.bootdo.order.utils;

import com.bootdo.common.redis.shiro.RedisManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Desc 订单缓存
 * @date 2019/12/13
 *
 */
@Component
public class OrderRedis {

    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.port}")
    private int port;

    private RedisManager redisManager;

    public OrderRedis(){
        redisManager = redisManager();
        redisManager.init();
    }

    /**
     * 配置redisManager
     * @return
     */
    private RedisManager redisManager() {
        RedisManager redisManager = new RedisManager();
        redisManager.setHost(host);
        redisManager.setPort(port);
        redisManager.setPassword(password);
        return redisManager;
    }

    /**
     * 为指定的key设置有效时间
     * @param key
     * @param time
     */
    public void expire(String key, int time) {
        redisManager.expire(key, time);
    }

    /**
     * 添加订单缓存
     * @param key
     */
    public void set(String key, String value){
        redisManager.set("order" + key, value, 5 * 60);
    }

    public String get(String key){
        String json = redisManager.get("order" + key);
        del(key);
        return json;
    }

    /**
     * 删除订单缓存
     * @param key
     */
    public void del(String key){
        redisManager.del("order" + key);
    }

}
